
clear all 

//define folder global here

gl Inputs "$folder\1_Inputs"
gl Intermediate "$folder\2_Intermediate"
gl Outputs "$folder\3_Outputs"


********************************************************************************
**# PREPARE DATA FOR EVENT STUDY GRAPHS - FIGURES 2, 3, 4
* AND SUPPLEMENTARY FIGURES C.1, E.1, E.2, E.3
********************************************************************************

**** PREPARE NY DATA
use "${Inputs}\NY_labor_eventstudy.dta", clear 
append using "${Inputs}\NY_homelessness_eventstudy.dta"
append using "${Inputs}\NY_hospital_eventstudy.dta"
append using "${Inputs}\NY_moves_eventstudy.dta"
append using "${Inputs}\NY_credit_eventstudy.dta"
append using "${Inputs}\NY_nbhood_eventstudy.dta"

	replace outcome = outcomevar if !mi(outcomevar) & mi(outcome)
	drop outcomevar

	gen 	outcome_clean = "Earnings" if outcome =="earn"
	replace outcome_clean = "Employment" if outcome =="emply"
	replace outcome_clean = "Any Homelessness Services" if outcome=="Any Homeless System Use"
	replace outcome_clean = outcome if mi(outcome_clean)
	replace outcome_clean = subinstr(outcome_clean, "Num", "Number of", .)
	replace outcome_clean = subinstr(outcome_clean, "Visit", "Visits", .)
	replace outcome_clean = "Number of Mental Health Visits" if outcome_clean=="Number of Mental Health"
	replace outcome_clean = "Any Auto Loan or Lease" if outcome=="autoloan"
	replace outcome_clean = "Credit Score" if outcome=="creditscore"
	replace outcome_clean = "Total balance: collections and delinquencies" if outcome=="delinquency" | outcome=="deliquency"
	replace outcome_clean = "No Open Revolving Account" if outcome=="zerorevolving"
	replace outcome_clean = "Financial Health Index" if outcome=="finindex"
	 
	drop outcome 
	rename (outcome_clean notevicted) (outcome non_evicted)
	gen geo = "ny"

	tempfile nyc_clean 
	save `nyc_clean'

********************************************************************************
**** PREPARE COOK DATA

import delimited "${Inputs}\Cook_lehd_hmis_marf_eventstudy.csv", clear

	replace outcome = "Emergency Shelter Use" if outcome=="em_shelter"
	replace outcome = "Any Homelessness Services" if outcome=="shelter"
	replace outcome = "Earnings" if outcome=="earn"
	replace outcome = "Employment" if outcome=="anyearn_allst"
	replace outcome = "Not at Eviction Address" if outcome=="not_at_eviction_address"
	replace outcome = "Neighborhood Poverty" if outcome=="avg_marf_all_hh_pov_rate" 
	replace outcome = "Eviction in a Different State" if outcome=="any_diff_state_evic"
	replace outcome = "Observed this Year" if outcome=="obs_this_year"
	
	gen 	time = relative_quarter
	replace time = relative_year if mi(relative_quarter)

	drop data relative* 

	tempfile cook_outcomes_part1
	save `cook_outcomes_part1'
	

import delimited "${Inputs}\Cook_experian_eventstudy.csv", clear 

tempfile cook_files
save `cook_files'


import delimited "${Inputs}\Cook_payday_accounts_eventstudy.csv", clear 
	rename (*1* *2* *3*) (*9* *10* *11*)
	merge 1:1 q_plot using `cook_files', assert(3) nogen

	tempfile cook_files2
	save `cook_files2'
	
	
import delimited "${Inputs}\Cook_payday_inquiries_eventstudy.csv", clear

	rename (*1* *2*) (*7* *8*)
	merge 1:1 q_plot using `cook_files2', assert(3) nogen

	keep q_plot var*_name var*_q_est var*_qxevict_est
	rename (var*_name var*_q_est var*_qxevict_est) (name_* est_* qxest_*)

	reshape long name_ est_ qxest_ , i(q_plot) j(num)
	drop num 
	replace est_ = 0  if mi(est_) & q_plot=="-12"
	replace qxest_ = 0  if mi(qxest_) & q_plot=="-12"
	 
	preserve 
		keep if regexm(q_plot, "Evict coefficient")
		rename est_ evic_coef 
		keep evic_coef name_ 
		
		tempfile evic_coefs
		save `evic_coefs'
	restore 
		
	preserve 
		keep if regexm(q_plot, "Non-evict mean t=-12")
		rename est_ non_evic_mean 
		keep non_evic_mean name_ 
		
		tempfile ne_means
		save `ne_means'
	restore 		
	
	drop if regexm(q_plot, "vict")
	destring q_plot, replace 
	
	merge m:1 name_ using `evic_coefs', assert(3) nogen
	merge m:1 name_ using `ne_means', assert(3) nogen
		
	gen evicted = est_ + qxest_ + evic_coef + non_evic_mean
	gen non_evicted = est_ + non_evic_mean
	
	drop est_ qxest_ evic_coef non_evic_mean 
	rename (q_plot name_) (time outcome)
	
	sort outcome time
	 
	append using `cook_outcomes_part1'
	
	gen geo = "cc"

*******************************************************************************	
** COMBINE AND CLEAN

append using `nyc_clean'
  
replace outcome = "Total Balance: Col. and Del." if outcome=="deliquency" | outcome=="Total balance: collections and delinquencies"
replace outcome = "Total Payday Borrowing" if outcome=="Total Payday Borrowing (Conditional on >0)"
replace outcome = "Financial Health Index" if outcome=="Financial Health"

reshape wide evicted non_evicted , i(time outcome) j(geo) string

foreach i in evicted non_evicted {
	gen `i'both = (`i'cc + `i'ny) / 2
}

reshape long evicted non_evicted, i(time outcome) j(geo) string 
drop if mi(evicted) & mi(non_evicted)
 
drop if regexm(outcome, "Total Balance|Credit|Borro|Auto|Open|Inde") & time < -8
drop if regexm(outcome, "Total Balance|Credit|Borro|Auto|Open|Inde") & time > 20 
 
replace outcome = outcome + "_comb" if geo=="both"
drop if regexm(outcome, "Payday") & time < -8
drop if regexm(outcome, "Payday") & time > 12

order geo outcome time, first 
sort geo outcome time
compress 
 
save "${Intermediate}\Event Studies.dta", replace


********************************************************************************
********************************************************************************
**# PREPARE FIGURE 6
********************************************************************************

use "${Intermediate}\Combined IV OLS.dta", clear

	drop if geo == "both" // these are both
	keep if inlist(time, "Q01_04", "Q05_08")
	keep if inlist(spec, "ivac", "ldv")
	keep if group=="all"
	drop group 

	replace geo = "c" if geo=="chi"
	replace geo = "n" if geo=="ny"
	replace time = subinstr(time, "Q", "q", .)
	replace time = subinstr(time, "_", "", .)
	replace spec = "IV" if spec=="ivac"
	replace spec = "OLS" if spec=="ldv"
	 
	gen 	keeping = 1 if inlist(outcome, "earn", "empl", "move", "auto", "cred", "zero")
	replace keeping = 1 if inlist(outcome, "deli", "eshu", "home", "inde", "povr")
	keep if keeping==1
	drop keeping

	* rescale the coefs
	gen resc_coef = evic_coeff * (sd_frac_evic / sd_outcome_full)

	keep outcome time_period geography resc_coef spec
 
	gen 	Outcome = "Earnings" if outcome=="earn"
	replace Outcome = "Employment" if outcome=="empl"
	replace Outcome = "Not at eviction address" if outcome=="move"
	replace Outcome = "Any auto loan or lease" if outcome=="auto"
	replace Outcome = "Credit score" if outcome=="cred"
	replace Outcome = "No open revolving account" if outcome=="zero"
	replace Outcome = "Total balance: collections and delinquencies" if outcome=="deli"
	replace Outcome = "Emergency shelter" if outcome=="eshu"
	replace Outcome = "Any homelessness services" if outcome=="home"
	replace Outcome = "Financial health index" if outcome=="inde"
	replace Outcome = "Neighborhood poverty rate" if outcome=="povr"
	
	drop if mi(Outcome)
	rename (spec resc_coef) (Specification value)
	
	reshape wide value, i(Outcome Specification time) j(geo) string
	drop if mi(valuec) | mi(valuen)
	
	gen Group = "Housing" if inlist(outcome, "eshu", "home", "povr", "move")
	replace Group = "Labor" if inlist(outcome, "empl", "earn")
	replace Group = "Financial" if mi(Group)
	
save "${Intermediate}\CC NY Comparison.dta", replace



********************************************************************************
********************************************************************************
**# PREPARE SUPPLEMENTARY FIGURES F.1 AND F.2 
********************************************************************************

** DID ESTIMATES 
use "${Intermediate}\Combined for forloops.dta", clear

	keep if spec=="did"
	keep if geo=="n" | geo=="c"
	keep if group=="a"
	keep if item=="co" | item=="se"
	keep if time=="q0108"
	keep if inlist(outcome, "earn", "empl")
		
	drop group spec time 
	isid outcome value geo
	
	reshape wide value, i(outcome geo) j(item) string
	rename (valueco valuese) (beta se)

	replace beta = subinstr(beta, "*", "", .)
	replace se = subinstr(se, "(", "", .)
	replace se = subinstr(se, ")", "", .)

	destring beta se, replace
	
	replace outcome = outcome + "_NY" if geo=="n"
	replace outcome = outcome + "_CC" if geo=="c"
	drop geo 
	
	gen labels = "Diff-in-Diff"
	
	tempfile did_boxplot 
	save `did_boxplot'
	
** NEW YORK ESTIMATES 
use "${Inputs}\NY_barchart_regs.dta", clear 

	replace outcome = "earn_NY" if outcome=="avgearn0t2"
	replace outcome = "empl_NY" if outcome=="qemployed0t2"
	
	gen 	labels = "Evicted v Benefits" if comparison=="Bene v Evicted, Basic Controls"
	replace labels = "Evicted v Non-Evicted" if comparison=="Not Evicted v  Evicted, Basic Controls"
	
	drop if mi(labels)
	keep labels outcome beta se 
	
	tempfile ny_boxplot
	save `ny_boxplot'
 
 
** COOK ESTIMATES 
import delimited "${Inputs}\Cook_barchart_regs.csv", clear 

	replace outcome = "empl" if outcome=="anyearn_allst"
	
	gen 	labels = "Evicted v ACS" if sample=="acs_renter" & controls=="some_ctrls"
	replace labels = "Evicted v Non-Evicted" if sample=="not_evicted" & controls=="some_ctrls"
	
	drop if mi(labels)
	rename (est_samp1_minus_samp2 std_error) (beta se)
	keep outcome labels beta se
	replace outcome = outcome + "_CC"
	
	append using `ny_boxplot'	
	append using `did_boxplot'
	
	gen		id = "1" if labels=="Evicted v ACS" | labels=="Evicted v Benefits"
	replace id = "2" if labels=="Evicted v Non-Evicted" 
	replace id = "3" if labels=="Diff-in-Diff"
	
	sort outcome id labels 
	order outcome id labels, first 

	compress 
	save "${Intermediate}\Boxplots.dta", replace
	
